﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using LEDContract;
using LED;
using LEDHelper;
namespace LED.Test
{
    public partial class Form1 : Form
    {
        public IntPtr hdc4;
        #region========LED Sender.dll==============
        [DllImport("InsigmaLEDDriver.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 InsigmaLEDDriver_Init();

        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 SetRect(ref SRECT rect, Int32 left, Int32 top, Int32 right, Int32 bottom);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_Open(ref PDeviceParam param, Int32 Notify, Int32 Window, Int32 Message);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_Query(Int32 dev, Byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_Close(Int32 dev);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_AdjustTime(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SendToScreen(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetPower(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SetPower(Int32 dev, byte Address, string Host, UInt16 port, int Power);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetBrightness(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SetBrightness(Int32 dev, byte Address, string Host, UInt16 port, byte Brightness);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetNotifyMessage(PNotifyMessage Notify);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_GetOption(int Index);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_SetOption(int Index, Int32 Value);
        /// <summary>
        ///   MakeRoot说明
        ///   Function  MakeRoot(RootType: Integer; ScreenType: Integer): Integer; Stdcall; External LEDSender;
        ///   RootType: 可选ROOT_PLAY或ROOT_DOWNLOAD
        ///    ROOT_PLAY: 表示显示内容仅播放，无需保存，控制卡掉电后不再需要显示
        ///    ROOT_DOWNLOAD: 表示显示内容需要保存在主板中，当控制卡上电后自动播放
        ///    (注意：控制卡只能保存1M的数据)
        ///    ScreenType: 必须和控制卡的设置相同，否则将不会显示
        /// </summary>
        /// <param name="RootType"></param>
        /// <param name="ScreenType"></param>
        /// <returns></returns>
        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 MakeRoot(Int32 RootType, Int32 ScreenType);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddLeaf(Int32 DisplayTime); //DisplayTime单位为毫秒

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddWindow(IntPtr hdc, Int32 width, Int32 height, ref SRECT rect, Int32 method, Int32 speed, Int32 transparent);
        //public static extern Int32 AddWindow(HDC dc, short width, short height, LPRECT rect, Int32 method, Int32 speed, Int32 transparent);
        //long (_stdcall *AddWindow)(HDC dc,short width, short height, LPRECT rect, long method, long speed, long transparent);
        //long (_stdcall *AddWindow)(HDC dc,short width, short height, LPRECT rect, long method, long speed, long transparent);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddDateTime(ref SRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddString(string str, ref SRECT rect, Int32 method, Int32 speed, Int32 transparent, Int32 fontset, Int32 fontcolor);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddText(string str, ref SRECT rect, Int32 method, Int32 speed, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddMovie(string filename, ref SRECT rect, Int32 stretch);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddCountUp(ref SRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format, LPSYSTEMTIME starttime);

        [DllImport("LEDSender.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddCountDown(ref SRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format, LPSYSTEMTIME endtime);
         
   
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref SRECT lpRect);
        [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        private static extern bool DeleteObject(IntPtr hObject);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest); 
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref System.Windows.Rect lpRect);
        private enum TernaryRasterOperations : uint
        {
            /// <summary>dest = source</summary> 
            SRCCOPY = 0x00CC0020,
            /// <summary>dest = source OR dest</summary> 
            SRCPAINT = 0x00EE0086,
            /// <summary>dest = source AND dest</summary> 
            SRCAND = 0x008800C6,
            /// <summary>dest = source XOR dest</summary> 
            SRCINVERT = 0x00660046,
            /// <summary>dest = source AND (NOT dest)</summary> 
            SRCERASE = 0x00440328,
            /// <summary>dest = (NOT source)</summary> 
            NOTSRCCOPY = 0x00330008,
            /// <summary>dest = (NOT src) AND (NOT dest)</summary> 
            NOTSRCERASE = 0x001100A6,
            /// <summary>dest = (source AND pattern)</summary> 
            MERGECOPY = 0x00C000CA,
            /// <summary>dest = (NOT source) OR dest</summary> 
            MERGEPAINT = 0x00BB0226,
            /// <summary>dest = pattern</summary> 
            PATCOPY = 0x00F00021,
            /// <summary>dest = DPSnoo</summary> 
            PATPAINT = 0x00FB0A09,
            /// <summary>dest = pattern XOR dest</summary> 
            PATINVERT = 0x005A0049,
            /// <summary>dest = (NOT dest)</summary> 
            DSTINVERT = 0x00550009,
            /// <summary>dest = BLACK</summary> 
            BLACKNESS = 0x00000042,
            /// <summary>dest = WHITE</summary> 
            WHITENESS = 0x00FF0062
        }

        [DllImport("gdi32.dll")]
        private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); 
 
        #endregion
        public int imi = 0;
        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(Form1_Load);
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == LEDAPI.WM_LED_NOTIFY)
            {
                PNotifyMessage msg = new PNotifyMessage();
                string content = string.Empty;

                //if (LED_GetNotifyMessage(ref msg) != 0)
                {
                    switch (msg.Message)
                    {
                        case LEDAPI.LM_TX_COMPLETE:
                            content = "发送完成";
                            break;
                        case LEDAPI.LM_RESPOND:
                            switch (msg.Command)
                            {
                                case LEDAPI.PKC_QUERY:
                                    content = "查询状态成功";
                                    break;
                                case LEDAPI.PKC_GET_POWER:
                                    content = (msg.Status == LEDAPI.LED_POWER_ON) ? "电源已打开" : "电源已关闭";
                                    break;
                                case LEDAPI.PKC_SET_POWER:
                                    if (msg.Result == 99)
                                        content = "处于自动开关屏模式,不能进行电源操作!";
                                    else if (msg.Status == LEDAPI.LED_POWER_ON)
                                        content = "电源已打开";
                                    else if (msg.Status == LEDAPI.LED_POWER_OFF)
                                        content = "电源已关闭";
                                    break;
                                case LEDAPI.PKC_GET_BRIGHT:
                                    content = "显示屏亮度为:" + msg.Status;
                                    break;
                                case LEDAPI.PKC_SET_BRIGHT:
                                    content = "亮度调整完毕";
                                    break;
                                case LEDAPI.PKC_ADJUST_TIME:
                                    content = "时钟校正完成";
                                    break;
                            }
                            break;
                        case LEDAPI.LM_TIMEOUT:
                            content = "传送超时";
                            break;
                    }
                    OnLedResponse(content, msg);
                }
            }
            else
            {
                base.WndProc(ref m);    //如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理
            }
        }
        private void OnLedResponse(string content, PNotifyMessage msg)
        {
            try
            {
                switch (msg.Message)
                {
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
            }
        }
        void Form1_Load(object sender, System.EventArgs e)
        { 
            timer1.Interval = 300;
            timer1.Start();
            text2.Text = imageList1.Images.Count.ToString(); 
            InsigmaLEDDriver_Init(); 
        }
        public IntPtr Capture()
        {
            IntPtr screenDC = GetDC(IntPtr.Zero);
            IntPtr memDC = CreateCompatibleDC(screenDC);
            Bitmap bmp = new Bitmap(@"D:\SXDFile\A3.jpg");
            IntPtr hBitmap = bmp.GetHbitmap();
            hBitmap = bmp.GetHbitmap();
            SelectObject(memDC, hBitmap); // Select bitmap from compatible bitmap to memDC 
            Send2Screen(memDC);            
            DeleteObject(hBitmap);
            ReleaseDC(IntPtr.Zero, screenDC);
            ReleaseDC(IntPtr.Zero, memDC);
            return screenDC;
        }

        private void Send2Screen(IntPtr hdc)
        {
            try
            {
                #region device
                Int32 dev = -1;
                PDeviceParam param = new PDeviceParam();
                param.devType = LEDAPI.DEVICE_TYPE_UDP;
                param.ComPort = 4;
                param.Speed = LEDAPI.SBR_57600;
                param.FlowCon = 0;
                dev = LED_Open(ref param, 2, 1, 1);
                SRECT srect = new SRECT();
                MakeRoot(LEDAPI.ROOT_PLAY, LEDAPI.SCREEN_COLOR);
                #endregion
                AddLeaf(1000);
                SetRect(ref srect, 0, 0, 280, 32);
                AddWindow(hdc, 280, 32, ref srect, 1, 1, 1);
                //ReleaseDC(hdc, IntPtr.Zero);
                LED_SendToScreen(dev, 1, "192.168.0.101", 6666);  //发送到1组1号显示屏，此处需要根据现场情况修改*/
            }
            catch (Exception ex)
            { 
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            { 
                //Bitmap bmp = new Bitmap(@"C:\temp\QQ.bmp");
                //Graphics g = Graphics.FromImage(bmp); 
                //IntPtr hdc = GetDC(this.pictureBox1.Handle);
                //IntPtr hdc = CreateCompatibleDC(IntPtr.Zero);

                //System.Windows.Rect rect = new System.Windows.Rect(); 
                Capture(); 
  /*
                AddLeaf(1000);
                SetRect(ref srect, 0, 0, 280, 32);
                hdc = GetDC(this.pictureBox2.Handle); 
                AddWindow(hdc, 280, 32, ref srect, 1, 1, 1);
                ReleaseDC(hdc, IntPtr.Zero);
              
                 1.对于同步屏，可以将一个窗口的句柄给显示器。用窗口模拟显示器。
                 *2.对于闪烁，可以增加一个AddLeaf(1000);黑屏一段时间
                 *
                pictureBox2.Image = Image.FromFile(@"C:\temp\QQ3.bmp");
               pictureBox2.Refresh();
               AddLeaf(1000);
               AddLeaf(1000);
               SetRect(ref srect, 0, 0, 57, 32);
               AddWindow(hdc, 50, 32, ref srect, 1, 1, 1);
               ReleaseDC(hdc, IntPtr.Zero);

               SetRect(ref srect, 60, 10, 127, 32);
             
               pictureBox1.Image = Image.FromFile(@"C:\temp\QQ2.bmp");
               pictureBox1.Refresh();
               hdc = GetDC(pictureBox1.Handle); 
               AddWindow(hdc, 50, 32, ref srect, 1, 1, 1);
               ReleaseDC(hdc, IntPtr.Zero);
               */
                
            }
            catch (Exception ex)
            {
               
            }
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            return;
            timer1.Stop();
            if (imi < imageList1.Images.Count)
            {
                pictureBox1.Image = imageList1.Images[imi];
                pictureBox1.Refresh();
                imi++;
                text1.Text = imi.ToString();
                button1_Click(null, null);
            }
            else
            {
                imi = 0;
            }
            timer1.Enabled = true;
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle),
         button2.ClientRectangle);
        }
    }
}
